package com.enniswu.weitter;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.enniswu.weitter.base.BaseActivity;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.moment.MomentFragment;
import com.enniswu.weitter.notification.NotificationFragment;
import com.enniswu.weitter.user.info.UserInfoFragment;
import com.enniswu.weitter.widget.adapter.FragmentPagerAdapter;

import java.util.Arrays;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.vp_main)
    ViewPager vpMain;
    @BindView(R.id.bnv_main)
    BottomNavigationView bnvMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        User user = DBSupport.getInstance().getCurrentUser();
        if (TextUtils.isEmpty(SPSupport.getInstance().getSessionId()))
            user = null;
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
                Arrays.asList(new MomentFragment(), new NotificationFragment(),
                        UserInfoFragment.newInstance(user)));
        vpMain.setOffscreenPageLimit(4);
        vpMain.setAdapter(adapter);
        bnvMain.setOnNavigationItemSelectedListener(itemSelectedListener);
        SPSupport.getInstance().putFirst();

    }

    private BottomNavigationView.OnNavigationItemSelectedListener itemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.item_moment:
                    vpMain.setCurrentItem(0);
                    return true;
                case R.id.item_notification:
                    vpMain.setCurrentItem(1);
                    return true;
                case R.id.item_user_center:
                    vpMain.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };
}
